package inputPackage;

import java.awt.CardLayout;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

import viewPackage.KaPow;

import mainPackage.GFrame;
import modelPackage.Bear;
import modelPackage.FlyingBear;
import modelPackage.Homework;
import modelPackage.Player;

public class KeyboardManager3D implements KeyListener {
	private Player player;
	private GFrame gameFrame;
	
	private int moveRight3DKey;
	private int moveLeft3DKey;
	private int moveUp3DKey;
	private int moveDown3DKey;
	private int jumpKey;
	private int switchViewsKey;
	private int pauseKey;
	

	public KeyboardManager3D(Player p, GFrame frame) {
		player = p;
		gameFrame = frame;
		
		moveRight3DKey = gameFrame.getKeyResource().getRight3D();
		moveLeft3DKey = gameFrame.getKeyResource().getLeft3D();
		moveUp3DKey = gameFrame.getKeyResource().getUp3D();
		moveDown3DKey = gameFrame.getKeyResource().getDown3D();
		jumpKey = gameFrame.getKeyResource().getJump();
		switchViewsKey = gameFrame.getKeyResource().getSwitchViews();
		pauseKey = gameFrame.getKeyResource().getPause();
		
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == moveRight3DKey) {
			player.setPosZVelocity(true);
		}
		else if (e.getKeyCode() == moveLeft3DKey) {
			player.setNegZVelocity(true);
		}
		else if (e.getKeyCode() == moveUp3DKey) {
			player.setPosXVelocity(true);
		}
		else if (e.getKeyCode() == moveDown3DKey) {
			player.setNegXVelocity(true);
		}
		
		else if (e.getKeyCode() == jumpKey) {
			if (player.getYVelocity() == 0 && player.getJumped() == false) {
				player.setYVelocity(-2);
				player.setJumped(true);
			}
		}
		else if (e.getKeyCode() == switchViewsKey) {
			player.setMode(2);
			player.setPosXVelocity(false);
			player.setNegXVelocity(false);
			player.setPosZVelocity(false);
			player.setNegZVelocity(false);
			player.setYVelocity(0);
			
			
			CardLayout cl2 = (CardLayout) gameFrame.getContentPane().getLayout();
			
			/*
			player.setPaused(true);
			cl2.show(gameFrame.getContentPane(), "kapow");
			gameFrame.getKaPow().update();
			gameFrame.repaint();
			JPanel kapow = (JPanel) (gameFrame.getContentPane().getComponent(3));
			kapow.grabFocus();
			Long startTime = System.currentTimeMillis();
			while (System.currentTimeMillis() - startTime < 2000) {
				gameFrame.getKaPow().paintComponent(gameFrame.getKaPow().getGraphics());
				//gameFrame.getKaPow().repaint();
			}
			player.setPaused(false);
			*/
			
				
			cl2.show(gameFrame.getContentPane(), "Game");
			JPanel game = (JPanel) (gameFrame.getContentPane().getComponent(0));
			game.grabFocus();
		}
		
		else if (e.getKeyCode() == pauseKey) {
			player.setPaused(true);
			player.setReturnTo(3);
			CardLayout cl = (CardLayout) gameFrame.getContentPane().getLayout();
			cl.show(gameFrame.getContentPane(), "Pause");
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		if (e.getKeyCode() == moveRight3DKey) {
			player.setPosZVelocity(false);
		}
		else if (e.getKeyCode() == moveLeft3DKey) {
			player.setNegZVelocity(false);
		}
		else if (e.getKeyCode() == moveUp3DKey) {
			player.setPosXVelocity(false);
		}
		else if (e.getKeyCode() == moveDown3DKey) {
			player.setNegXVelocity(false);
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		
	}
	
}
